<!DOCTYPE HTML>
<html>
<head>
    <title> 搜索表单</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link href="/static/bui/css/dpl.css" rel="stylesheet"/>
    <link href="/static/bui/css/bui.css" rel="stylesheet"/>

    <!--<link href="http://g.alicdn.com/bui/bui/1.1.21/css/default/dpl.css" rel="stylesheet"/>-->
    <!--<link href="http://g.alicdn.com/bui/bui/1.1.21/css/default/bui.css" rel="stylesheet"/>-->

</head>
<body>

<div class="content" style="padding-left: 10px;">
    <div class="row">
        <form id="searchForm" class="form-horizontal" tabindex="0" style="outline: none;">
            <label class="control-label">用户名称：</label>
            <input type="text" class="control-text" name="name"/>
            <button id="btnSearch" type="submit" class="button button-primary">搜索</button>
        </form>
    </div>
    <div id="grid"></div>
</div>


<div id="content" class="hide">
    <form id="J_Form" class="form-horizontal">
        <input type="hidden" name="a" value="3"/>
        <div class="row">
            <div class="control-group span8">
                <label class="control-label"><s>*</s>用户名</label>
                <div class="controls">
                    <input name="name" type="text" data-rules="{required:true}" class="input-normal control-text"/>
                </div>
            </div>
            <div class="control-group span8">
                <label class="control-label"><s>*</s>账号</label>
                <div class="controls">
                    <input name="account" type="grade" data-rules="{required:true}" class="input-normal control-text"/>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span8">
                <label class="control-label"><s>*</s>状态：</label>
                <div class="controls">
                    <select  data-rules="{required:true}"  name="state" class="input-normal">
                        <option value="">请选择</option>
                        <option value="1">可用</option>
                        <option value="0">屏蔽</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span15" style="height: 75px;">
                <label class="control-label">描述：</label>
                <div class="controls">
                    <textarea name="description" class="input-large" type="text"/>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span15" style="height: 75px;">
                <label class="control-label">角色：</label>
                <div id="s1" class="controls" style="height: 58px">
                    <input type="hidden" id="roleIds" name="roleIds"/>
                </div>
            </div>
        </div>
    </form>
</div>

<script src="/static/bui/js/sea.js"></script>
<script src="/static/bui/js/config.js"></script>


<script src="/static/bui/js/jquery-1.8.1.min.js"></script>
<script src="/static/bui/js/bui.js"></script>


<script type="text/javascript">

    //select多选角色
    var Select = BUI.Select,
            Data = BUI.Data;

    var store = new Data.Store({
                url : 'role/enabledList',
                autoLoad : true
            }),
            select = new Select.Select({
                render:'#s1',
                valueField:'#roleIds',
                multipleSelect : true,
                store : store
            });
    select.render();
    select.on('change', function(ev){
//        alert(ev.item.text);
    });


    var data;
    $.ajax({
        url: "user/list",
        type: 'GET',
        dataType: 'json',
        timeout: 25000,
        error: function () {
            alert('Error ');
        },
        success: function (result) {
            data = result.rows;
        }
    });

    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            enumState = {"1":"可用","0":"屏蔽"},
            columns = [
                {title:'用户id',dataIndex:'id',width:160},
                {title:'用户名称',dataIndex:'name',width:100},
                {title:'账号',dataIndex:'account',width:100},
                {title:'状态',dataIndex:'state',width:60,renderer:BUI.Grid.Format.enumRenderer(enumState)},
                {title:'创建时间',dataIndex:'createTime',width:150,renderer:BUI.Grid.Format.datetimeRenderer},
                {title:'描述',dataIndex:'description',width:300},
                {title:'操作',dataIndex:'',width:200,renderer : function(value,obj){
                    var editStr1 = '<span class="grid-command btn-edit" title="编辑用户信息">编辑</span>';//页面操作不需要使用Search.createLink
                    return editStr1;
                }}
            ],
            editing = new Grid.Plugins.DialogEditing({
                contentId : 'content', //设置隐藏的Dialog内容
                triggerCls : 'btn-edit', //触发显示Dialog的样式
                editor : {
                    title: '编辑',
                    listeners : {
                        show: function () {
                            select.on('onclick', function(ev){
                                alert(ev.item.text);
                            });
                        }
                    }
                },
                autoSave : true
            }),
            store = new Store({
                url: 'user/list',
                proxy : {
                    save : { //也可以是一个字符串，那么增删改，都会往那么路径提交数据，同时附加参数saveType
                        updateUrl : 'user/edit',
                        addUrl : 'user/add',
                        removeUrl : 'user/del'
                    }
                },
                autoSync : true, //保存数据后，自动更新
                autoLoad:true,
                pageSize:10
            }),
            grid = new Grid.Grid({
                render:'#grid',
                loadMask: true,
                forceFit: true,
                columns : columns,
                store: store,
                plugins : [Grid.Plugins.CheckSelection,Grid.Plugins.AutoFit], //勾选插件、自适应宽度插件
                tbar:{// 底部工具栏
                    items : [{
                        btnCls : 'button button-small',
                        text : '<i class="icon-plus"></i>添加',
                        listeners : {'click' : addFunction}
                    },
                    {
                        btnCls : 'button button-small',
                        text : '<i class="icon-remove"></i>删除',
                        listeners : {'click' : delFunction}
                    }]
                },
                bbar : {// 顶部工具栏
                    pagingBar:true
                },
                plugins : [editing,BUI.Grid.Plugins.CheckSelection,BUI.Grid.Plugins.AutoFit] // 插件形式引入多选表格
            });

    //保存或者读取失败
    store.on('exception',function (ev) {
        var e = ev.error;
        BUI.Message.Alert('出现异常:'+JSON.stringify(e.jqXHR.status));
    });

    grid.render();

    function addFunction(){
        var type = {saveType : 'add'}; //标志是新增加的记录
        editing.add(type,'name'); //添加记录后，直接编辑
    }

    //删除操作
    function delFunction(){
        var selections = grid.getSelection();
        delItems(selections);
    }

    function delItems(items){
        var ids = [];
        BUI.each(items,function(item){
            ids.push(item.id);
        });

        if(ids.length){
            BUI.Message.Confirm('确认要删除选中的记录么？',function(){
                store.save('remove',{ids : ids});
            },'question');
        }
    }

    //监听事件，删除一条记录
    grid.on('cellclick',function(ev){
        var sender = $(ev.domTarget); //点击的Dom
        if(sender.hasClass('btn-del')){
            var record = ev.record;
            delItems([record]);
        }
    });











//    var Select = BUI.Select;
//    $.ajax({
//        url: "role/list",
//        type: 'GET',
//        dataType: 'json',
//        timeout: 25000,
//        error: function () {
//            alert('Error ');
//        },
//        success: function (result) {
//            var select = new Select.Combox({
//                render:'#s1',
//                showTag:true,
////                limit : 10, //限制最多3个
//                width: 310,
//                valueField : '#hide',//显示tag的Combox必须存在valueField
//                items: result,
//                forbitInput : true, //只能从列表中选择，输入无效,一般用于suggest中
//                tagFormatter : function(value){ //将id : text中的id去掉
//                    var arr = value.split(':');
//                    return arr[1]; //用户输入
//                },
//                list : { //自定义列表
//                    textGetter : function(item){
//                        return item.id + ':' + item.text;
//                    },
//                    idField : 'id' //默认是value
//                }
//            });
//            select.render();
//        }
//    });

    //创建表单，表单中的日历，不需要单独初始化
    var form = new BUI.Form.HForm({
        srcNode : '#searchForm'
    }).render();

    form.on('beforesubmit',function(ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });


</script>

</body>
</html>
